let api = require('../../../api/index');
let http=require("../../../utils/http")
Page({
    data: {
        image: '',
        list: [],
        form: {
            page: 1,
            pagesize: 10,
        },
        pageStatus: 1 // 1下拉加载更多..  5加载中... 10没有更多了... 99暂无数据
    },
    onLoad: function (options) {
        this.reload()
    },
    onShow: function () {

    },
    onReady: function () {

    },
    onHide: function () {

    },
    onUnload: function () {

    },
    onPullDownRefresh: function () {
        console.log('打印日志，要不然会出现一个版本bug')

        wx.startPullDownRefresh()

        setTimeout(() => {
            this.getList('reset')

            // 停止下拉刷新
            wx.stopPullDownRefresh()
        }, 0)
    },
    onReachBottom: function () {
        this.getList()
    },
    // onShareAppMessage: function () {

    // },
    reload() {
        console.log('初次加载')
        this.getList('reset')
        this.getImage()
    },
    getImage(){
        let that = this
        api.doctor.getActivityImage().then(res => {
            console.log(res)
            res.data.ResData.map( e => {
                console.log(e)
                if(e.type == 3){
                    that.setData({
                        image: e.image
                    })
                }
            })
        })
    },
    getList(type) {
        if (type === 'reset') {
            this.setData({
                ['form.page']: 1,
                // list: [],
                pageStatus: 1
            })
        }


        if (this.data.pageStatus !== 1) {
            return false
        } else {
            this.setData({
                pageStatus: 5
            })
        }

        api.bargain.getBargainList({
            page: this.data.form.page,
            pagesize: this.data.form.pagesize,
            type: this.data.active
        }).then(res => {
            let list = res.data.ResData.Items
            let pageSize = res.data.ResData.ItemsPerPage
            let pageStatus;


            let time = parseInt(new Date().getTime() / 1000)
            list.map((item, index) => {
                if (item.start_time <= time && time <= item.end_time) {
                    item.isBuy = true
                } else {
                    item.isBuy = false
                }

                return item
            })

            if (this.data.list.pageNum > 1) {
                list = this.data.list.concat(list);
            }

            if (list.length < pageSize) {
                if (list.length <= 0) {
                    pageStatus = 99
                } else {
                    pageStatus = 10
                }
            } else {
                pageStatus = 1
            }

            this.setData({
                list: list,
                pageStatus: pageStatus,
                ['form.page']: this.data.form.page++,
            })
        })
    },
    toPagesDetails(e) {
        let info = e.currentTarget.dataset.item;
        console.log(info);
        wx.setStorageSync('bargainId', info.id)
        if (info.isBuy === true) {
            wx.navigateTo({
                url: `/pages/product/detail/detail?source=kanjia&id=${info.id}`,
            })
        }
    }
})